import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;


public class LZEncode 
{
	public static String getLZ(String input){
		LZNode root=new LZNode(0);
		int i=0; 
		int nextIndex=1;
		char c='*';
		LZNode current,father;
		String output="";
		String pair="";
		input=input.replace(" ", "").toLowerCase();
		System.out.println("\nWorking on: "+input+"("+input.length()+") .... \n");
		while (i<input.length()){
			current=root;
			father=root;
			while (current!=null && i<input.length()){
				c=input.charAt(i);
				//System.out.println("looking for "+c);
				father=current;
				current=current.getChild(c);
				i++;
			}
			if (current==null){
				father.addChild(c, nextIndex);
				nextIndex++;
				pair="("+father.myIndex+","+c+")";
			}
			else{
				pair="("+current.myIndex+",*)";
			}
			
			//print
			output=output+pair;
			System.out.print(pair);	
		}
		return output;
	}
	
	static String fileString="";
	
	public static void main(String[] args)
	{
		
		//String input;
		//input="Hasta la victoria siempre";
		//input="aacgacgata";
		//input="aa";
		//input="abcdefghijklmnopqrstuvwxyz";
		
		String inputFile  = args[0]; 
		String outputFile = args[1]; 
		//String inputFile  =  ".\\src\\Example1\\input1.txt";; 
		//String outputFile = inputFile+"_4_processed.txt"; 
		
		read_From_File(inputFile);
		System.out.println("\nGot from file: "+fileString);
		
		String toWriteString=getLZ(fileString);
		write_To_File(outputFile, toWriteString);
		
		
		
		
		
		
		//TODO
		/*
		 *  Here you should call your
		 *  implementation that encodes a Text given in inputFile
		 *  according to the Lempel-Ziv algorithm,
		 *  and outputs the encoded text to the outputFile 
		 *  
		 *  
		 */
	}
	private static void write_To_File(String outputFilename,String encodedText) {

        System.out.println("Writing to file " + outputFilename);
        
        
        try {
            File outFile = new File(outputFilename);
            FileWriter ofw = new FileWriter(outFile);
            System.out.println("writing....\n");
            System.out.print(encodedText);
            ofw.write(encodedText);
            ofw.close();
            
            System.out.println("\nWrote to file " + outputFilename);
            
        }
        catch (Exception e) {
            System.out.println( "Error \""+ e.toString()+ "\" on file "+outputFilename);
            e.printStackTrace() ;
            System.exit(-1) ;      //brutally exit the program
        }

    }
	
	public static void read_From_File(String inputFilename){
       	System.out.println("Reading from file " + inputFilename);
       
       	try {
            File inFile = new File(inputFilename);
            FileReader ifr = new FileReader(inFile);
            BufferedReader ibr= new BufferedReader(ifr) ;

            String line = "" ;
            while (line != null )
            {
                line = ibr.readLine() ;
                if (line != null)
                {
                    StringTokenizer st = new StringTokenizer(line); // splits the line into tokens
					
                    while(st.hasMoreTokens())
                    {
                    	String token = st.nextToken().trim();
                    	System.out.println(token);
                    	fileString=fileString+token;
                    }                   
                }
            }

            ibr.close();
            ifr.close();
        }

        catch(Exception e) {
            System.out.println( "Error \""+ e.toString()+ "\" on file "+inputFilename);
            e.printStackTrace() ;
            System.exit(-1) ;      //brutally exit the program
        }
	
	}

}
